package org.noootsab.gae.games.tzaar.log {
import mx.core.mx_internal;
import mx.logging.ILogger;
import mx.logging.LogEvent;
import mx.logging.LogEventLevel;
import mx.logging.targets.LineFormattedTarget;
import mx.rpc.remoting.RemoteObject;

use namespace mx_internal;

public class ServerTraceTarget extends LineFormattedTarget {
    public var logService:RemoteObject;

    public function ServerTraceTarget() {
        super();

        // change the defaults for these
        super.fieldSeparator = "##";
        super.includeTime = true;
        super.includeDate = true;
        super.includeCategory = true;
        super.includeLevel = true;
        super.filters = ["org.noootsab.*"];
        super.level = LogEventLevel.ALL;
    }

    public override function logEvent(event:LogEvent):void {
        // Take care to NOT include mx.*, as code in std lib issues log calls,
        // which will call this target, which will send a message to server
        // (through rpc operation), which will log to this again, ... until StackOverflowException
        var category:String = ILogger(event.target).category;
        if (category.indexOf("mx.") != 0) {
            super.logEvent(event);
        }
    }

    /**
     *  Super class calls this method when it has formatted the message
     *  and wants to send it somewhere.
     *  The default implementation does nothing.
     */
    mx_internal override function internalLog(message:String):void {
        if (logService != null) {
            logService.log(message);
        }
    }
}
}